﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CGLib.Enums;
using CGLib;

namespace TienLen.UC
{
    public partial class MenuMain : UserControl
    {
        MainMenu parentfrm;
        readonly string[] ButtonText = new string[]
        { "Play through servers", "Play through MSN Live", "Play offline / LAN",
          "Single Player", "Local Area Network (LAN)", "Tutorial"};

        public MenuMain(MainMenu mainfrm)
        {
            InitializeComponent();
            parentfrm = mainfrm;

            button1.MouseLeave += new EventHandler(parentfrm.Button_MouseLeave);
            button2.MouseLeave += new EventHandler(parentfrm.Button_MouseLeave);
            button3.MouseLeave += new EventHandler(parentfrm.Button_MouseLeave);
        }

        internal void Back()
        {
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;

            button1.Text = ButtonText[0];
            button2.Text = ButtonText[1];
            button3.Text = ButtonText[2];

            parentfrm.BackButton.Visible = false;
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            switch (button1.Text)
            {
                case "Play through servers": parentfrm.InfoText = "Play through Tien Len's official servers"; return;
                case "Single Player": parentfrm.InfoText = "Play with computer players of varying difficulty"; return;
            }
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            switch (button2.Text)
            {
                case "Play through MSN Live": parentfrm.InfoText = "Play through your MSN account with your contacts"; return;
                case "Local Area Network (LAN)": parentfrm.InfoText = "Play with local players through the local network"; return;
            }
        }

        private void button3_MouseEnter(object sender, EventArgs e)
        {
            switch (button3.Text)
            {
                case "Play offline / LAN": parentfrm.InfoText = "Play offline with AI's or through LAN"; return;
                case "Tutorial": parentfrm.InfoText = "Learn to play Tien Lien"; return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "Play offline / LAN")
            {
                button1.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;

                button1.Text = ButtonText[3];
                button2.Text = ButtonText[4];
                button3.Text = ButtonText[5];

                parentfrm.BackButton.Visible = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            switch (button1.Text)
            {
                case "Single Player":
                    {
                        CGLib.IGame selGame;
                        GameSelection GameSelectFrm = new GameSelection();
                        PlayerRoom PlayerRoomFrm;
                        Player[] players;

                        if (GameSelectFrm.DialogResult == DialogResult.Cancel)
                            return;

                        if (GameSelectFrm.ShowDialog() == DialogResult.OK)
                        {
                            selGame = GameSelectFrm.selectedGame;
                            PlayerRoomFrm = new PlayerRoom(selGame);

                            if (PlayerRoomFrm.ShowDialog() == DialogResult.OK)
                            {
                                players = new Player[PlayerRoomFrm.Players.Count];

                                for (int x = 0; x < players.Length; x++)
                                {
                                    players[x] = new Player(PlayerRoomFrm.Players[x].Text, PlayerRoomFrm.Players[x].Tag != null ? UserType.Computer : UserType.Host
                                        , x + 1, null);

                                    if (players[x].Type == UserType.Computer)
                                    {
                                        players[x].AIFunc = (CGLib.IComputer)PlayerRoomFrm.Players[x].Tag;
                                        players[x].Name += " - " + x.ToString();
                                    }
                                }

                                parentfrm.SuppressExit = true;
                                parentfrm.Close();

                                Client client = new Client(selGame, SvrType.Offline, players, null);
                                client.Show();
                            }
                        }

                        break;
                    }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            switch (button2.Text)
            {
                case "Play through MSN Live":
                    {
                        parentfrm.AddControl(new MSNLogin(parentfrm));
                        break;
                    }
            }
        }

    }
}
